/**
 * 文件名：StateGridStatisticServlet.java
 *
 * 创建人：LS - ls121989@163.com
 *
 * 创建时间：2016年4月29日 上午11:10:36
 *
 * 版权所有：LS
 */
package fnic.prehand.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import fnic.jsontrans.AgentUrl;
import fnic.prehand.common.JacksonFormatService;
import fnic.prehand.esagent.ap_measure_new2.StateGridInnerStatement;
import fnic.prehand.esagent.ap_measure_new2.StateGridSearch;

/**
 * [描述信息：说明类的基本功能]
 *
 * @author LS - ls121989@163.com
 * @version 1.0 Created on 2016年4月29日 上午11:10:36
 */
@SuppressWarnings("serial")
public class StateGridStatisticServlet extends HttpServlet {

	private static final Logger LOG = Logger.getLogger(StateGridStatisticServlet.class);

	private static final JacksonFormatService jsonInstance = JacksonFormatService.getInstance();

	private StateGridSearch stateGrid = new StateGridSearch();

	private StateGridInnerStatement stateInner = new StateGridInnerStatement();

	private static final String START = "start";

	private static final String END = "end";

	private static final String TYPE = "type";

	private static final String DIRECTION = "direction";

	private static final String ADDRESS = "address";

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String opt = request.getRequestURI();
		String httpbody = getHttpBody(request.getInputStream());
		String searchRes = null;
		if (opt.equals(AgentUrl.STATISTICS_SEARCH)) {
			String start = null;
			String end = null;
			String type = null;
			String direction = null;
			String address = null;
			@SuppressWarnings("unchecked")
			Map<String, Object> params = (Map<String, Object>) jsonInstance.String2Obj(httpbody, "java.util.Map");
			if (null == params) {
				searchRes = "[]";
			} else {
				if (params.get(START) != null) {
					start = String.valueOf(params.get(START));
				}
				if (params.get(END) != null) {
					end = String.valueOf(params.get(END));
				}
				if (params.get(TYPE) != null) {
					type = String.valueOf(params.get(TYPE));
				}
				if (params.get(DIRECTION) != null) {
					direction = String.valueOf(params.get(DIRECTION));
				}
				if (params.get(ADDRESS) != null) {
					address = String.valueOf(params.get(ADDRESS));
				}
				if (StringUtils.isBlank(start) || StringUtils.isBlank(end) || StringUtils.isBlank(type)
						|| StringUtils.isBlank(direction)) {
					searchRes = "[]";
				} else if (StringUtils.isBlank(address)) {
					try {
						searchRes = stateGrid.dataStatistic(start, end, direction, type);
					} catch (Exception e) {
						LOG.error("dataStatistic fail, " + e.getMessage());
						LOG.error(e.getMessage(), e);
					}
				} else {
					try {
						searchRes = stateInner.secondaryStatement(start, end, direction, type, address);
					} catch (Exception e) {
						LOG.error("secondaryStatement fail, " + e.getMessage());
						LOG.error(e.getMessage(), e);
					}
				}
			}
		}

		PrintWriter pw = null;
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setStatus(HttpServletResponse.SC_OK);
			pw = response.getWriter();
			pw.println(searchRes);
			pw.close();
		} catch (Exception e) {
			LOG.error("http rsp writer fail: " + e.getMessage());
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
		}
	}

	private String getHttpBody(InputStream stream) {
		InputStreamReader isr = null;
		BufferedReader streamReader = null;
		StringBuilder probedata = null;
		String httpbody = null;
		boolean succeedfg = true;

		try {
			String tempString;
			isr = new InputStreamReader(stream, "UTF-8");
			streamReader = new BufferedReader(isr);
			probedata = new StringBuilder();
			while ((tempString = streamReader.readLine()) != null) {
				probedata.append(tempString);
			}
		} catch (Exception e) {
			LOG.warn("get httpbody exception - " + e.getMessage());
			succeedfg = false;
		}
		if (succeedfg) {
			httpbody = probedata.toString();
		}
		try {
			if (streamReader != null) {
				streamReader.close();
			}
			if (isr != null) {
				isr.close();
			}
		} catch (IOException e) {
			LOG.warn("close stream exception - " + e.getMessage());
		}
		LOG.info("http body:" + httpbody);
		return httpbody;
	}

}
